package com.chen.solution.rpc.account.api.client;

import com.chen.solution.rpc.account.api.constant.AccountConstant;
import com.chen.solution.rpc.account.api.dto.*;
import com.chen.solution.rpc.commonlib.api.BaseResponse;
import com.chen.solution.rpc.commonlib.auth.AuthConstant;
import com.chen.solution.rpc.commonlib.validation.PhoneNumber;
import org.springframework.cloud.openfeign.FeignClient;
import org.springframework.web.bind.annotation.*;

import javax.validation.Valid;
import javax.validation.constraints.Min;
import javax.validation.constraints.NotBlank;

/**
 * 账户 REST 服务调用客户端
 * @author ChenTian
 * @date 2022/4/10
 */
@FeignClient(name = AccountConstant.SERVICE_NAME, path = AccountConstant.ACCOUNT_V1+"/account", url = "${rpc.account-service-endpoint}")
public interface AccountClient {

    /**
     * 创建新账户
     */
    @PostMapping(path = "/create")
    GenericAccountResponse createAccount(@RequestHeader(AuthConstant.AUTHORIZATION_HEADER) String authz, @RequestBody @Valid CreateAccountRequest request);

    /**
     * 获取或创建（如不存在）账户
     * GetOrCreate is for internal use by other APIs to match a user based on their phoneNumber or email.
     */
    @PostMapping(path= "/getOrCreate")
    GenericAccountResponse getOrCreateAccount(@RequestHeader(AuthConstant.AUTHORIZATION_HEADER) String authz, @RequestBody @Valid GetOrCreateRequest request);

    /**
     * 获取现有账户列表（内部使用）
     */
    @GetMapping(path = "/list")
    ListAccountResponse listAccounts(@RequestHeader(AuthConstant.AUTHORIZATION_HEADER) String authz, @RequestParam(value = "offset") int offset, @RequestParam(value = "limit") @Min(0) int limit);

    /**
     * 通过用户ID获取已有账户
     */
    @GetMapping(path = "/get")
    GenericAccountResponse getAccount(@RequestHeader(AuthConstant.AUTHORIZATION_HEADER) String authz, @RequestParam("userId") @NotBlank String userId);

    /**
     * 通过电话号码获取已有账户
     */
    @GetMapping(path = "/getAccountByPhoneNumber")
    GenericAccountResponse getAccountByPhoneNumber(@RequestHeader(AuthConstant.AUTHORIZATION_HEADER) String authz, @RequestParam("phoneNumber") @PhoneNumber String phoneNumber);

    /**
     * 更新账户
     */
    @PutMapping(path = "/update")
    GenericAccountResponse updateAccount(@RequestHeader(AuthConstant.AUTHORIZATION_HEADER) String authz, @RequestBody @Valid AccountDto newAccount);

    /**
     * 更新密码
     */
    @PutMapping(path = "/updatePassword")
    BaseResponse updatePassword(@RequestHeader(AuthConstant.AUTHORIZATION_HEADER) String authz, @RequestBody @Valid UpdatePasswordRequest request);

}
